import 'package:flui/flui.dart';
import 'package:lepig/model/BannerModel.dart';
import 'package:lepig/model/ProductModel.dart';
import 'package:lepig/model/SelectShopPlatformModel.dart';
import 'package:lepig/model/SpecialModel.dart';
import 'package:lepig/pages/webView/Web.dart';
import 'package:lepig/request/HttpBanner.dart';
import 'package:lepig/request/HttpSelectGaoyongjinLists.dart';
import 'package:lepig/request/HttpSelectShopPlatform.dart';
import 'package:lepig/request/HttpSpecial.dart';
import 'package:lepig/util/JumpUtil.dart';
import 'package:lepig/widget/GoodsHorizontalOne.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

import 'ShopBoxs.dart';
import 'Topic.dart';

class FirstPage extends StatefulWidget {
  @override
  _FirstPage createState() => new _FirstPage();
}

/**
 * 首页 tab 第一个
 */
class _FirstPage extends State<FirstPage>   with AutomaticKeepAliveClientMixin{
  ScrollController _scrollController = ScrollController(); //listview的控制器

  List<BannerModel> banners=[];
  List<SelectShopPlatformModel> platform=[];
  List<SpecialModel> specials=[];
  List<ProductModel> products=new  List<ProductModel>();

  //分页
  int page=1;
  bool _isloadLock=true;
  bool _isloaddone=false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getBanners();
    getSelectShopPlatform();
    getSelectSpecial();
    downRefresh();
    getJinXuan();

  }
  //获取banner
  getBanners(){
    HttpBanner(this.context,viewPosition: 100).then((value) {
        setState(() {
          banners=value;
        });
    });
  }
  //获取商城平台
  getSelectShopPlatform(){
    HttpSelectShopPlatform(this.context).then((value){
        setState(() {
          platform=value;
        });
    });
  }
  //获取专题
  getSelectSpecial(){
    HttpSpecial(this.context).then((value){
        setState(() {
          specials=value;
        });
    });
  }

  //下拉刷新
  downRefresh(){
    _scrollController.addListener(() {
      if (_scrollController.position.pixels ==
          _scrollController.position.maxScrollExtent) {
           print('滑动到了最底部');
          if(_isloadLock&&!_isloaddone){
            getJinXuan();
          }
        }
    });
  }

  //获取精选好货
  getJinXuan(){
    _isloadLock=false;
    HttpSelectGaoyongjinLists(this.context,page: page).then((value){
      _isloadLock=true;
      page=page+1;
      if(value==null||value.length==0){
        _isloaddone=true;//记载完毕
      }
      setState(() {
        products.addAll(value);
      });
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _scrollController.dispose();
  }

  @override
  bool get wantKeepAlive => true;
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Container(
      color: Color.fromRGBO(247, 247, 247,1),
      child: Padding(
        padding: EdgeInsets.only(left: 10,right: 10),
        child: ListView(
          controller:_scrollController ,
          children: [
            Container(
              height: 150,
              margin: EdgeInsets.only(top: 10.0),
              child:  null == banners || banners.isEmpty  ?  FLSkeleton(
                shape: BoxShape.rectangle,
                margin: EdgeInsets.only(top: 10, left: 10),
                height: 40,
              ):new Swiper(
                itemBuilder: (BuildContext context, int index) {
                  return GestureDetector(
                    child: ClipRRect(
                      //剪裁为圆角矩形
                        borderRadius: BorderRadius.circular(5.0),
                        child: Image.network(
                          banners[index].imgUrl,
                          fit: BoxFit.fill,
                          height: 150.0,
                        )),
                    onTap: (){
                      JumpUtil(context,jumpName:banners[index].title,jumpUrl: banners[index].jumpUrl ).go();
                    },
                  );
                },
                itemCount: banners.length,
              ),
            ),
            new ShopBoxs(lists:platform),
            new Topic(specials),
            //优选商品列表
            new Column(
              children: <Widget>[
                new Container(
                  margin: EdgeInsets.only(top: 10.0),
                  width: 150.0,
                  color: Colors.amber,
                  child: new Image.asset("images/other/jinxuan.png",width:150.0,),
                  alignment: Alignment.center,
                ),
              ],
            ),
            ...getProducts(),
            new Align(
              child: _isloadLock?new Text("加载中。。。"):SizedBox(),
            ),
            new Container(height: 10.0,)
          ],
        ),
      ),
    );
  }

  List<Widget> getProducts(){
    return products.map((element) {
      return  new GoodsHorizontalOne(element);
    }).toList();
  }
}

